
/**   
*    
* 项目名称：a_yongan   
* 类名称：OutsourcingInController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月19日 下午3:29:51    
* 修改时间：2018年6月19日 下午3:29:51   
* 修改备注：   
* @version    
*    
*/
package com.yongan.warehouse.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.util.SerialNumber;
import com.yongan.warehouse.entity.OtherGoodOut;
import com.yongan.warehouse.service.OtherGoodOutService;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/othergoodout")
public class OtherGoodOutController {

	@Autowired
	private OtherGoodOutService otherGoodOutService;
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String othergoodoutadd(Model model) {
		String number = SerialNumber.Getnum();
		model.addAttribute("number", number);
		return "othergoodoutadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> othergoodoutadddo( OtherGoodOut otherGoodOut,Model model) {
		System.out.println(otherGoodOut.toString());
		otherGoodOut.setOthergoodoutId(UUID.randomUUID().toString());
		Execution rs = otherGoodOutService.executeOtherGoodOut(otherGoodOut, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/othergoodoutlist", method = RequestMethod.GET)
	public String othergoodoutcontent(Model model,HttpServletRequest request) {
		List<OtherGoodOut> otherGoodOutlist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<OtherGoodOut> pb = new PageBean<OtherGoodOut>();
		
		totalRecords = otherGoodOutService.getCount();
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			otherGoodOutlist = otherGoodOutService.getOtherGoodOutList(start, end);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("othergoodoutlist", otherGoodOutlist);
		return "othergoodoutlist";
	}
	
	@RequestMapping(value = "/{othergoodoutId}/detail", method = RequestMethod.GET)
	public String othergoodoutdetail(@PathVariable("othergoodoutId") String othergoodoutId, Model model) {
		if (!(othergoodoutId.equals("")|othergoodoutId==null)) {
			OtherGoodOut otherGoodOut = otherGoodOutService.getOtherGoodOutById(othergoodoutId);
			System.out.println(otherGoodOut.getOthergoodoutFhckId()+"============");
			model.addAttribute("othergoodout", otherGoodOut);
		}
		return "othergoodoutdetail";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> othergoodoutupdate(OtherGoodOut otherGoodOut, Model model) {
		Execution rs = otherGoodOutService.executeOtherGoodOut(otherGoodOut, otherGoodOut.getOthergoodoutId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{othergoodoutId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> othergoodoutDelete(
			@PathVariable("othergoodoutId") String othergoodoutId, Model model) {
		if (!(othergoodoutId.equals("")|othergoodoutId==null)) {
			Execution rsdelete = otherGoodOutService.executeOtherGoodOut(null, othergoodoutId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
	
}
